package aex.util;



import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
/**
 * 全局常量在这里设置,
 * 这个程序会读取webBase.properties文件，需要存放到类路径下
 * @author JiangYouYao
 *
 */
public class InitWebContex extends HttpServlet   {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static boolean DEBUG_ON=true;
	public static int INDEX_PAGESIZE ;
	public static int DATA_PAGESIZE ;
	/**商品显示**/
	public static int NAV_SHOW_GRADE ;
	public static String CONFIG_PATH;
	public static String INIT_URL;
	
	
	
	
	
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * 初始化网站的配置文件
	 * @author JiangYouYao
	 * @date   2014年10月14日-上午8:20:59
	 */
	public void init() throws ServletException {
		Properties props = new Properties();
        try {
        	System.out.println(this.getClass().getResource("").getPath());
        	CONFIG_PATH=this.getClass().getResource("/webBase.properties").getPath();
            InputStream in = new BufferedInputStream(new FileInputStream(CONFIG_PATH));
            props.load(in);
            String deBugOn = props.getProperty("deBugOn");
            if(deBugOn.equals("true")){
            	DEBUG_ON=true;
            }else{
            	DEBUG_ON=false;
            System.out.println("非开发模式");
            }
            CU.deBugPrintln("初始化程序配置文件");
            INDEX_PAGESIZE=Integer.parseInt(props.getProperty("indexPageSize"));
            DATA_PAGESIZE=Integer.parseInt(props.getProperty("dataPageSize"));
            NAV_SHOW_GRADE=Integer.parseInt(props.getProperty("navShowGrade"));
            INIT_URL=props.getProperty("initUrl");
            
            
            
            //把导航栏数据放入到context中去
            this.getServletContext().setAttribute("navGrade", NAV_SHOW_GRADE);
            this.getServletContext().setAttribute("webTitle", props.getProperty("webTitle"));
            this.getServletContext().setAttribute("webDescription", props.getProperty("webDescription"));
            this.getServletContext().setAttribute("webKeyWord", props.getProperty("webKeyWord"));
            //启动一个的等待线程去访问更新网站的导航栏，一便在每一次启动tomcat的时候更新栏目
            /* final String webName=this.getServletContext().getContextPath();
            final String  ip=InetAddress.getLocalHost().getHostAddress();
           new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						CU.deBugPrintln("初始化网站线程启动。。。。");
						Thread.sleep(30*1000);
					} catch (InterruptedException e) {
						 TODO Auto-generated catch block
						e.printStackTrace();
					}
						 CU.sendGet(INIT_URL, "jyy=1");
				}
			}).start();*/
            
        } catch (Exception e) {
        	//配置文件初始化失败
        	System.out.println("配置文件初始化失败");
            e.printStackTrace();
        }
	}

	
}
